package com.greenhouse9.prog07;

import java.io.*;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javax.xml.parsers.*;
import org.xml.sax.SAXException;

public class ResponseManager
{
    private Document doc;
    private HashMap <Integer, Item> hmItem = new HashMap <Integer, Item> ();

    public HashMap <Integer, Item> getHmItem (){
        return hmItem;
    }

    public void load(String filename){

        try {

            DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dbfactory.newDocumentBuilder();

            doc = builder.parse(new File(filename));

        } catch (ParserConfigurationException e) {
            System.err.println(e);
        } catch (SAXException e) {
            System.err.println(e);
        } catch (IOException e) {
            System.err.println(e);
        }

    }

    public void parseItemYahoo(){

        Element root = doc.getDocumentElement();

        //yahoo�ˑ�
        NodeList listHit = root.getElementsByTagName("Hit");

        for (int i = 0; i < listHit.getLength() ; i++) {
            //System.out.println("Node " + i + ": " + listHit.item(i).getNodeName());  // for debug

            XmlObject xo = new XmlObject();
            xo.setXmlInfo(listHit.item(i));
            //xo.printR(0);  // for debug

            Item item = new Item ();
            item.yahooItemInfo = xo;
            item.init();

            hmItem.put(i, item);
        }

    }

}
